local string = string 

local mt = {}
setmetatable(string, mt)
mt.__index = mt 

--[剔掉字符串开始的空白字符]
function mt.trimleft(s)
    return (s:gsub('^%s+', ''))
end

--[剔掉字符串末尾的空白字符]
function mt.trimright(s)
    return (s:gsub('%s+$', ''))
end

--[剔掉字符串两端的空白字符]
function mt.trim(s)
    return s:trimleft():trimright()
end

--[裁剪字符串]
---@param s string          字符串
---@param pattern string    裁剪符号
---@param plain number      裁剪终点
---@param maxTokens number  裁剪次数
function mt.split(s, pattern, plain, maxTokens)
    local pos = 0
    local start, stop
    local r = {}
    while true do
        start, stop = s:find(pattern, pos, plain)
        if not start then
            break
        end
        table.insert(r, s:sub(pos, start - 1))
        pos = stop + 1
        if maxTokens then
            maxTokens = maxTokens - 1
            if maxTokens == 0 then
                break
            end
        end
    end
    table.insert(r, s:sub(pos))
    return r
end

function split_string(...)
    return mt.split(...)
end

--[取字符串哈希值]     
function mt.hash(str)
    return jass.StringHash(str)
end

--[文本格式]     
function mt.formatter(content, object, index)
    local gloss = object.gloss or "|cffffdd55"
    local level = index or 1 
    return (
        content:trim():gsub("(%|[cC]%w%w%w%w%w%w%w%w)(.-)(%|[Rr])", function (header, contenti, ender)
            local retval, count = contenti:gsub("%'(.-)([%w_]+)(.-)%'",function(symbol1, name, symbol2)
                symbol1 = symbol1 or ''
                symbol2 = symbol2 or ''
                local res = object[name] or name
                if type(res)  == 'table' then 
                    res = symbol1 .. (res[object.level or level] or '') .. symbol2
                elseif type(res) == 'function' then 
                    res = symbol1 .. res(object) .. symbol2
                elseif type(res) == 'number' then 
                    if math.type(res) == "integer" then 
                        res = (gloss .. '%s%.0f%s|r'):format(symbol1, res, symbol2)
                    else 
                        res = (gloss .. '%s%.02f%s|r'):format(symbol1, res, symbol2)
                    end
                    return  ender .. res .. header
                end 
                return res
            end)
            if count > 0 then 
                return header .. retval .. ender
            end 
        end):gsub("(%'.-%')", function (contento)
            local symbol1, name, symbol2 = contento:match("%'(.-)([%w_]+)(.-)%'")
            local res = object[name] or name
            if type(res)  == 'table' then 
                res = symbol1 .. (res[object.level or level] or '') .. symbol2
            elseif type(res) == 'function' then 
                res = symbol1 .. res(object) .. symbol2
            elseif type(res) == 'number' then 
                if math.type(res) == "integer" then 
                    res = (gloss .. '%s%.0f%s|r'):format(symbol1, res, symbol2)
                else 
                    res = (gloss .. '%s%.02f%s|r'):format(symbol1, res, symbol2)
                end
            end 
            return res
        end)
    )
end 

--格式化对象
function format_obj(object,res,level)
    if type(res)  == 'table' then 
        if (#res==0) then
            return res 
        end
        res = (res[object.level or level] or res[#res] or '')
    elseif type(res) == 'function' then 
        res = res(object)
    elseif type(res) == 'boolean' then 
        return res
    end
    if res==nil then
        return nil
    end
    if type(res) == 'number' then 
        local ex = math.ceil(res)
        if ex==res then
            return ex
        end
        return res
    end 
    if type(res) == 'string' then
        return res:gsub("'(.-)'",function(name)
            local data = object.data or object
            local res = data[name] or object[name] or name
            return format_obj(object,res,level or object.level)
        end)
    else
        return format_obj(object,res,level)
    end
end


return string